package org.kotemaru.gae.dstool.client;

import java.io.*;
import java.util.*;
import org.mortbay.log.Log;

public class ClientUtil {
	public static final String UTF8 = "utf-8";
	public static final String DSTOOL_ = "dstool.";
	
	
	public static Properties loadProps(String name, Properties props, boolean withError) throws IOException {
		//Properties props = new Properties();
		if (props == null) props = new Properties();
		InputStream in = null;
		try {
			in = new FileInputStream(name);
			props.load(in);
		} catch (IOException e) {
			if (withError) throw e;
			Log.warn(e.toString());
		} finally {
			if (in != null) in.close();
		}
		return props;
	}

	public static void saveProps(String name, Properties props) throws IOException {
		OutputStream out = new FileOutputStream(name);
		try {
			props.store(out, "");
		} finally {
			out.close();
		}
	}
	
	public static BufferedWriter takeWriter(String fname) throws IOException {
		OutputStream out = (fname == null) 
			? System.out
			: new FileOutputStream(fname);
		Writer writer = new OutputStreamWriter(out, UTF8);
		return new BufferedWriter(writer);
	}

	public static BufferedReader takeReader(String fname) throws IOException {
		InputStream in = (fname == null) 
			? System.in
			: new FileInputStream(fname);
		Reader reader = new InputStreamReader(in);
		return new BufferedReader(reader);
	}

	public static String correntAppid(String appid) {
		if (appid.indexOf('.')>=0) return appid;
		return DSTOOL_ + appid;
	}


}
